const express = require('express');
const { ApolloServer } = require('apollo-server-express');

const { typeDefs } = require('./graphql/schemas/schema');
const { resolvers } = require('./graphql/resolvers/resolver');

const context = () => {
  return {
    version: '1.0.0',
  };
};

async function startApolloExpressServer(typeDefs, resolvers, context) {
  const server = new ApolloServer({
    typeDefs,
    resolvers,
    context,
    cors: { credentials: true, allowedHeaders: '', origin: '*' },
  });

  const app = express();
  await server.start();
  server.applyMiddleware({ app, path: '/api/graphql' });

  app.listen(3000, () => {
    console.log(
      `Server is running in https://localhost:3000${server.graphqlPath}`
    );
  });
}

startApolloExpressServer(typeDefs, resolvers, context);
